import type { UserInfo, themeColor } from '/#/store';
import { defineStore } from 'pinia';
import { store } from '@/store';
import { constDataConfig } from '@/config/constData.config';
import { settingConfig } from '@/config/setting.config';

interface UserState {
  userInfo: UserInfo | null;
  accessToken: string;
  referrer: string;
  openId: string;
  globalConfig: any;
  refreshToken: string;
  cartNum: number;
  notifyNum: number;
  imHistory: any;
  imNoRead: number;
  isImPanel: boolean;
  isStuffService: boolean;
  themeColor: themeColor | null;
}

const ACCESSTOKEN = uni.getStorageSync('accessToken') || '';
const REFERRER = uni.getStorageSync('referrer') || '';
const USER = uni.getStorageSync('user') || {};
const REFRESHTOKEN = uni.getStorageSync('refreshToken') || '';
const GLOBALCONFIG = uni.getStorageSync('globalConfig') || {};
const CARTNUM = uni.getStorageSync('cartNum') || 0;
const NOTIFYNUM = uni.getStorageSync('notifyNum') || 0;
const IMHISTORY = uni.getStorageSync('imHistory') || [];
const IMNOREAD = uni.getStorageSync('imNoRead') || 0;
const THEMECOLOR = uni.getStorageSync('themeColor');
// const LOCALE = uni.getStorageSync('locale') || 'zh';
const { themeList } = constDataConfig;
export const useUserStore = defineStore({
  id: 'app-user',
  state: (): UserState => ({
    // user info
    userInfo: USER.member,
    // 用户token
    accessToken: ACCESSTOKEN,
    // 推荐人
    referrer: REFERRER,
    // 小程序openid
    openId: '',
    // 网络状态，用于下载提醒
    // networkState: 'unknown',
    globalConfig: GLOBALCONFIG,
    // 刷新token
    refreshToken: REFRESHTOKEN,
    // 购物车数字角标
    cartNum: CARTNUM,
    // 消息中心数字角标
    notifyNum: NOTIFYNUM,
    // 历史聊天消息，仅保存最后100条
    imHistory: IMHISTORY,
    // 当前未读聊天消息数量
    imNoRead: IMNOREAD,
    // 当前是否处于聊天面板
    isImPanel: false,
    // 当前是否人工客服服务
    isStuffService: false,
    themeColor: THEMECOLOR,
  }),

  getters: {
    getUserInfo() {
      return USER.member;
    },
    // 全局配置
    globalConfig() {
      return this.globalConfig;
    },
    // 全局主题
    themeColor() {
      let theme: any = this.themeColor;
      if (!theme) {
        theme = themeList.filter((item) => item.name === (settingConfig.styleType || 'rf'))[0];
      }
      return theme;
    },

    // 判断用户是否登录
    hasLogin() {
      return ACCESSTOKEN ? true : false;
    },
  },

  actions: {
    globalConfigChange({ commit }, info) {
      commit('setGlobalConfig', info);
    },
    // logout({ commit }, info) {
    //   commit('logout',info);
    // },
    login(provider) {
      this.accessToken = provider.access_token;
      this.refreshToken = provider.refresh_token;
      // this.userInfo = provider.member;
      this.userInfo = provider;
      uni.setStorageSync('user', provider);
      uni.setStorageSync('accessToken', provider.access_token);
      uni.setStorageSync('refreshToken', provider.refresh_token);
      uni.setStorageSync('userInfo', provider.member);
    },
    logout() {
      this.accessToken = '';
      this.refreshToken = '';
      this.userInfo = null;
      uni.removeStorageSync('accessToken');
      uni.removeStorageSync('refreshToken');
      uni.removeStorageSync('userInfo');
    },
    setReferrer(referrer) {
      this.referrer = referrer;
      uni.setStorageSync('referrer', referrer);
    },
    setOpenId(openId) {
      this.openId = openId;
      uni.setStorageSync('openId', openId);
    },
    // setNetworkState(state, provider) {
    //   state.networkState = provider;
    // },
    setGlobalConfig(provider) {
      this.globalConfig = provider;
      uni.setStorageSync('globalConfig', provider);
    },
    addImHistory({ commit }, provider) {
      if (provider) {
        let data = this.imHistory;
        data.push(provider);
        uni.setStorageSync('imHistory', data);
        this.imHistory = data;
        if (!this.isImPanel) {
          commit('addImNoRead', 1);
        }
      }
    },
    clearImHistory() {
      this.imHistory = [];
      uni.setStorageSync('imHistory', []);
      this.imNoRead = 0;
      uni.setStorageSync('imNoRead', 0);
    },
    addImNoRead(provider) {
      this.imNoRead = this.imNoRead + provider;
      uni.setStorageSync('imNoRead', this.imNoRead);
    },
    setIsImPanel(provider) {
      this.isImPanel = provider;
      if (provider) {
        this.imNoRead = 0;
        uni.setStorageSync('imNoRead', 0);
      }
    },
    setIsStuffService(provider) {
      this.isStuffService = provider;
    },
    setThemeColor(val) {
      this.themeColor = val;
      uni.setStorageSync('themeColor', val);
    },
  },
});
export function useUserStoreWithOut() {
  return useUserStore(store);
}
